package org.example.datasource;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.util.Properties;

public class PooledDataSourceFactory implements DataSourceFactory {
    private DataSource dataSource;

    public PooledDataSourceFactory() {
        this.dataSource = new PooledDataSource();
    }
    public void setProperties(Properties props) {
        //使用反射动态给DataSource设置值
        try {
            for (Method method : dataSource.getClass().getDeclaredMethods()) {
                for (Object key : props.keySet()) {
                    String proName = (String) key;
                    if(method.getName().toLowerCase().endsWith("set"+proName)){
                        method.invoke(dataSource, props.getProperty(proName));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public DataSource getDataSource() {
        return dataSource;
    }
}
